const pkg = require("./package");
const config = require("./config/conf");
const router = require("./config/router").routers;
const CompressionPlugin = require("compression-webpack-plugin");
const TerserPlugin = require("terser-webpack-plugin");
const path = require("path");
const fs = require("fs");

console.log("=======================================");
console.log("You are running on " + process.env.NODE_ENV + " mode");
console.log("=======================================");

module.exports = {
  mode: "universal",
  server: {
    port:3018,
  },

  router: {
    middleware: ["redirect", "checkuser", "root", "states", "i18n"],
    extendRoutes(routes, resolve) {
      routes.push.apply(routes, router(resolve));
    },
    // 全局设置切换路由页面回到顶部
    scrollBehavior (to, from, savedPosition) {
      return { x: 0, y: 0 }
    }
  },
  /*
   ** Headers of the page
   */
  head: {
    title: config.title,
    meta: [
      { charset: "utf-8" },
      {
        name: "viewport",
        content:
          "width=device-width,minimum-scale=1.0,initial-scale=1.0,maximum-scale=1.0,viewport-fit=cover,user-scalable=no"
      },
      { hid: "description", name: "description", content: pkg.description },
      { name: "screen-orientation", content: "portrait" },
      { name: "x5-orientation", content: "portrait" },
      { name: "msapplication-tap-highlight", content: "no" },
      // { name: "keyword", content: "北京泓深网校,北京泓深,泓深,网校,网校搭建" }
    ],
    script: [
      { src: "/fonts/iconfont.js" },
      // { src: "/js/jweixin-1.2.0.js" },
      // { src: "https://res.wx.qq.com/open/js/jweixin-1.6.0.js" },
	  { src: "/js/jweixin-1.6.0.js" },
      { src: "/js/qrcode.min.js" },
      { src: "/js/aliyun-webrtc-sdk-1.12.1.js" },
      { src: "/js/sha256.js" },
      { src: "/js/fastclick.js" },
      { src: "/js/lib-generate-test-usersig.min.js" },
      { src: "/js/trtc.js" },
      { src: "/js/jigsaw.min.js" },
	  { src: "/js/f2.min.js" },
    ]
    // link: [
    //   { rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }
    // ]
  },

  /*
   ** Customize the progress-bar color
   */
  loading: { color: "#fff" },

  /*
   ** Global CSS
   */
  css: [
    "@/assets/css/iview.scss",
    "@/assets/css/vant.scss",
    { src: "@/assets/css/swiper.scss" },
    "@/static/fonts/iconfont.css",
    "@/assets/css/main.scss",
    "quill/dist/quill.snow.css",
    "quill/dist/quill.bubble.css",
    "quill/dist/quill.core.css",
    "@/assets/css/richtext.scss"
  ],

  /*
   ** Plugins to load before mounting the App
   */
  plugins: [
    // { src: "@/plugins/babel-polyfill", ssr: true },
    { src: "~/plugins/httpfunc.js", ssr: true },
    { src: "~/plugins/message.js", ssr: true },
    { src: "~/plugins/dragging.js", ssr: true },
    { src: "~/plugins/vue-map.js", ssr: true },
    {
      src: "~/plugins/vant.js",
      ssr: true
    },
    { src: "~/plugins/iview.js", ssr: true },
    { src: "~/plugins/vconsole.js", ssr: false },
    { src: "~/plugins/vue-animate-number.js", ssr: false },
    "~/plugins/i18n.js",
    {
      src: "~/plugins/vue-quill-editor",
      ssr: false
    },
    { src: "~/plugins/lazyload.js", ssr: true },
    { src: "~/plugins/vue-pdf.js", ssr: false }
  ],

  /*
   ** Nuxt.js modules
   */
  modules: [
    // Doc: https://github.com/nuxt-community/axios-module#usage
    "@nuxtjs/axios",
    "@gauseen/nuxt-proxy"
  ],
  /*
   ** Axios module configuration
   */
  axios: {
    // See https://github.com/nuxt-community/axios-module#options
    proxy: true
  },
  proxyTable: {
    "/api": {
      target: config.baseUrl,
      ws: true,
      changeOrigin: true,
      pathRewrite: { "^/api": "/" }
    }
  },
  cache: true,
  // or
  // cache: {
  //   max: 1000,
  //   maxAge: 900000
  // },
  env: {
    NODE_ENV: process.env.NODE_ENV || "production",
  },
  /*
   ** Build configuration
   */
  build: {
    // publicPath:'', //cdn地址
    /*
     ** You can extend webpack config here
     */
    //打包分析
    // analyze: true,
    // assetFilter: function(assetFilename) {
    //   return assetFilename.endsWith(".js");
    // },
    transpile: [/^iview/],
    plugins:
      process.env.NODE_ENV == "production"
        ? [
            new CompressionPlugin({
              test: /\.js$|\.css$/, // 匹配文件名
              algorithm: "gzip",
              threshold: 0, // 对超过xxx的数据进行压缩
              deleteOriginalAssets: false // 是否删除原文件
            })
          ]
        : [],
    babel: {
      plugins: [
        [
          "import",
          {
            libraryName: "iview",
            libraryDirectory: "src/components"
          }
        ]
      ]
    },
    optimization: {
      splitChunks: {
        chunks: "all",
        automaticNameDelimiter: ".",
        maxAsyncRequests: 7,
        minSize: 0,
        maxSize: 200000
      },
      minimize: process.env.NODE_ENV == "production" ? true : false
    },
    extend(config, ctx) {
      config.output.globalObject = 'this'
      config.module.rules.push(
        {
          test: /\.pdf$/,
          loader: 'url-loader'
        }
      )
      config.module.rules.push({
        test: /\.swf$/,
        loader: "url-loader",
        options: {
          limit: 10000
        }
      });
      if (!ctx.isDev) {
        config.plugins.push(
          new TerserPlugin({
            terserOptions: {
              compress: {
                // drop_console: true,
                drop_debugger: true,
                pure_funcs: ["console.log", "console.warn"]
              }
            },
            sourceMap: true,
            cache: true,
            parallel: true
          })
        );
      }
    }
  }
};
